import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:logger/logger.dart';
import 'package:permission_handler/permission_handler.dart';

Logger logger = Logger();

class Homepageinappweb extends StatefulWidget {
  const Homepageinappweb({super.key});

  @override
  State<Homepageinappweb> createState() => _HomepageinappwebState();
}

class _HomepageinappwebState extends State<Homepageinappweb> {
  String filePath = 'assets/html/web.html';
  InAppWebViewController? webViewController;
  InAppWebViewSettings settings = InAppWebViewSettings(
      isInspectable: true,
      mediaPlaybackRequiresUserGesture: false,
      allowsInlineMediaPlayback: true,
      iframeAllow: "camera; microphone",
      iframeAllowFullscreen: true);

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InAppWebView(
      onWebViewCreated: (controller) {
        webViewController = controller;
        loadLocalHtml();
      },
      onPermissionRequest: (controller, request) async {
        Map<Permission, PermissionStatus> status = await [Permission.camera,Permission.microphone].request();
        var deniedPermissions = status.entries.where((entry) => entry.value == PermissionStatus.denied);
        if (deniedPermissions.isNotEmpty) {
          return PermissionResponse(resources: request.resources, action: PermissionResponseAction.DENY);
        } else {
          return PermissionResponse(resources: request.resources, action: PermissionResponseAction.GRANT);
        }
      },
      onConsoleMessage: (controller, consoleMessage) {
        logger.d(consoleMessage);
      },
    );
  }

  void loadLocalHtml() async {
    // await setJsHandler();
    String html = await rootBundle.loadString(filePath);
    webViewController?.loadData(data: html);
  }

  // Future<void> setJsHandler() async {
  //   if (defaultTargetPlatform != TargetPlatform.android || await WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
  //     await webViewController?.addWebMessageListener(WebMessageListener(
  //       jsObjectName: "Flutter_Interface",
  //       onPostMessage: (message, sourceOrigin, isMainFrame, replyProxy) {
  //         logger.d("inappweb 收到 $message");
  //         processJsMessage(message?.data);
  //       },
  //     ));
  //   }
  // }

  // /// 处理收到的js消息
  // void processJsMessage(String message) async {
  //   var map = jsonDecode(message);
  //   logger.d("map is $map ,code = ${map["code"]}");
  //   switch (map["code"]) {
  //     case "3":
  //       //跳转RTC
  //       Navigator.push(context, MaterialPageRoute(builder: (context) => const RtcPage()));
  //       break;
  //     case "4":
  //       //跳转RTC Meet
  //       Navigator.push(context, MaterialPageRoute(builder: (context) => MeetRoomPage(map["userId"],map["roomId"])));
  //       break;
  //   }
  // }
}
